import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import { DrawService } from './draw.service';
import { DrawFilterDto, QueryDrawHistoryDto } from './dto';
import { ApiResponse } from '../../common/interfaces/api-response.interface';
import {
  DrawResponse,
  DrawStats,
  RestaurantDrawCount,
} from '../../common/types/response.types';
import { Restaurant } from '../../entities/restaurant.entity';
import { DrawHistory } from '../../entities/draw-history.entity';

@Controller('draw')
export class DrawController {
  constructor(private readonly drawService: DrawService) {}

  // 开始抽签
  @Post('start')
  async startDraw(@Body() filters?: DrawFilterDto) {
    const data = await this.drawService.startDraw(filters);
    return {
      success: true,
      data,
      message: '抽签成功',
    } as ApiResponse<DrawResponse>;
  }

  // 重新抽签
  @Post('restart')
  async redraw(@Body() filters?: DrawFilterDto) {
    const data = await this.drawService.redraw(filters);
    return {
      success: true,
      data,
      message: '重新抽签成功',
    } as ApiResponse<DrawResponse>;
  }

  // 获取符合条件的餐厅
  @Post('eligible')
  async getEligibleRestaurants(@Body() filters?: DrawFilterDto) {
    const data = await this.drawService.getEligibleRestaurants(filters);
    return {
      success: true,
      data,
    } as ApiResponse<Restaurant[]>;
  }

  // 获取抽签历史
  @Get('history')
  async getDrawHistory(@Query() query: QueryDrawHistoryDto) {
    const data = await this.drawService.getDrawHistory(query);
    return {
      success: true,
      data,
    } as ApiResponse<typeof data>;
  }

  // 获取最近抽签历史
  @Get('history/recent')
  async getRecentDrawHistory(@Query('limit') limit?: number) {
    const data = await this.drawService.getRecentDrawHistory(limit);
    return {
      success: true,
      data,
    } as ApiResponse<DrawHistory[]>;
  }

  // 获取今天抽签历史
  @Get('history/today')
  async getTodayDrawHistory() {
    const data = await this.drawService.getTodayDrawHistory();
    return {
      success: true,
      data,
    } as ApiResponse<DrawHistory[]>;
  }

  // 获取本周抽签历史
  @Get('history/week')
  async getWeekDrawHistory() {
    const data = await this.drawService.getWeekDrawHistory();
    return {
      success: true,
      data,
    } as ApiResponse<DrawHistory[]>;
  }

  // 获取本月抽签历史
  @Get('history/month')
  async getMonthDrawHistory() {
    const data = await this.drawService.getMonthDrawHistory();
    return {
      success: true,
      data,
    } as ApiResponse<DrawHistory[]>;
  }

  // 获取抽签统计
  @Get('stats')
  async getDrawStats() {
    const data = await this.drawService.getDrawStats();
    return {
      success: true,
      data,
    } as ApiResponse<DrawStats>;
  }

  // 获取餐厅抽签次数统计
  @Get('restaurant-counts')
  async getRestaurantDrawCounts(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
    @Query('limit') limit?: number,
  ) {
    const data = await this.drawService.getRestaurantDrawCounts(
      startDate,
      endDate,
      limit,
    );
    return {
      success: true,
      data,
    } as ApiResponse<RestaurantDrawCount[]>;
  }

  // 清空抽签历史
  @Delete('history/clear')
  async clearDrawHistory() {
    await this.drawService.clearDrawHistory();
    return {
      success: true,
      data: null,
      message: '抽签历史清空成功',
    } as ApiResponse<null>;
  }

  // 删除抽签历史
  @Delete('history/:id')
  async deleteDrawHistory(@Param('id') id: string) {
    await this.drawService.deleteDrawHistory(id);
    return {
      success: true,
      data: null,
      message: '抽签历史删除成功',
    } as ApiResponse<null>;
  }
}
